package cc.cary.warehouse.controller;

import cc.cary.warehouse.common.R;
import cc.cary.warehouse.dto.UserDTO;
import cc.cary.warehouse.entity.User;
import cc.cary.warehouse.form.UserDeleteForm;
import cc.cary.warehouse.form.UserQueryForm;
import cc.cary.warehouse.form.UserSaveForm;
import cc.cary.warehouse.service.IUserService;
import cc.cary.warehouse.untils.Beans;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

/**
 * @author Cary
 * @date 2023/4/18
 */
@Tag(name = "用户管理", description = "用户相关接口啦")
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @Operation(summary = "用户列表")
    @PostMapping("/pages")
    public R<IPage<UserDTO>> pages(@Valid @RequestBody UserQueryForm form) {
        return userService.pages(form);
    }

    @Operation(summary = "用户添加")
    @PostMapping("/save")
    public R<String> save(@Valid @RequestBody UserSaveForm form) {
        userService.save(Beans.copy(form, User.class)
                .setCreateTime(new Date()));
        return R.ok("添加成功");
    }

    @Operation(summary = "用户删除")
    @PostMapping("/delete")
    public R<String> delete(@Valid @RequestBody UserDeleteForm form) {
        userService.removeBatchByIds(form.getUserIds());
        return R.ok("删除成功");
    }
}
